<?php

defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * Description of Admin
 *
 * @author bxsp
 */
class Admin extends MY_Controller {

    // 站点配置
    private $website_config;

    public function __construct() {
        parent::__construct();
        $this->load->library('parser');
        $this->load->model('Admin_model', 'Admin');
        $this->website_config = $this->Admin->getWebSiteConfig();
    }

    // 后台管理首页
    public function dashboard() {
        $this->load->view('/admin/dashboard', $this->website_config);
    }

    // 站点设置
    public function site_config() {
        $this->load->view('/admin/site_config', $this->website_config);
    }

    // 设置站点配置
    public function ajax_set_site_config() {
        $postdata = $this->input->post();
        $ret = $this->Admin->setWebSiteConfig($postdata);
        $this->output->set_output(TRUE);
    }

    // 标签管理
    public function tags_config() {
        $tags_data['tags'] = $this->Admin->getTagsConfig();
        $this->load->view('/admin/tags_config', $tags_data);
    }

    public function ajax_set_tags_config() {
        $tag_name = $this->input->post('tag_name');
        $ret = $this->Admin->setTagsConfig($tag_name);
        echo $ret;
    }

    public function ajax_del_tag() {
        $tag_id = $this->input->post('tag_id');
        $this->db->where('tag_id', $tag_id);
        $ret = $this->db->update('ci_tags_config', array('tag_status' => 0));
        echo $ret;
    }

    public function ajax_open_tag() {
        $tag_id = $this->input->post('tag_id');
        $this->db->where('tag_id', $tag_id);
        $ret = $this->db->update('ci_tags_config', array('tag_status' => 1));
        echo $ret;
    }

    public function ajax_close_tag() {
        $tag_id = $this->input->post('tag_id');
        $this->db->where('tag_id', $tag_id);
        $ret = $this->db->update('ci_tags_config', array('tag_status' => 2));
        echo $ret;
    }

    // 添加标签测试数据
    public function addTestTagsData() {
        $data = array(
            'tag_create_time' => time(),
        );
        for ($i = 0; $i <= 30; $i++) {
            $data['tag_name'] = '标签' . $i;
            $this->db->insert('ci_tags_config', $data);
        }
    }

    // 分类管理
    public function cat_config() {
        $cat_data['cats'] = $this->Admin->getCatConfig();
        $this->load->view('/admin/cat_config', $cat_data);
    }

    public function ajax_load_catTree() {
        $this->db->order_by('cat_id', 'DESC');
        $this->db->where_not_in('cat_status', 0);
        $query = $this->db->get('ci_cat_config')->result_array();
        $tmp = array();
        $tmp['cats'] = $this->treeArray($query);
        $catTreeTmp = $this->parser->parse('/admin/tmpl/catTree', $tmp, TRUE);
        echo $catTreeTmp;
//        echo json_encode($ret, TRUE);
    }

    // 无限级分类树
    function treeArray($data) {
        $result = array();
        foreach ($data as $val) {
            if ($val['p_id'] == 0) {
                $val['child'] = $this->getTreeChild($val['cat_id'], $data);
                $result[] = $val;
            }
        }
        return $result;
    }

    function getTreeChild($pid, $data) {
        $result = array();
        foreach ($data as $val) {
            if ($val['p_id'] == $pid) {
                $val['child'] = $this->getTreeChild($val['cat_id'], $data);
                $result[] = $val;
            }
        }
        return $result;
    }

    public function ajax_set_cat_config() {
        $p_id = $this->input->post('p_id');
        isset($p_id) ? $query['p_id'] = $p_id : $query['p_id'] = 0;
        $query['cat_name'] = $this->input->post('cat_name');
        $ret = $this->Admin->setCatConfig($query);
        echo $ret;
    }

    // 博文管理
    public function post_config() {

        // 分类数据
        $data['cats'] = $this->db->get_where('ci_cat_config', array('cat_status' => 1))->result_array();
        // 标签数据
        $data['tags'] = $this->db->get_where('ci_tags_config', array('tag_status' => 1))->result_array();

        // 博文数据
        $data['post'] = $this->db->get_where('ci_post', array('post_status' => 1))->result_array();

        $this->load->view('/admin/post_config', $data);
    }

    // 新增文章
    public function ajax_set_post() {
        $data['post_title'] = $this->input->post('title');
        $data['cat_id'] = $this->input->post('cat');
        $tags = $this->input->post('tag');
        $tags == TRUE ? $data['tag_id'] = implode(',', $tags) : $data['tag_id'] = '';
        $data['post_content'] = $this->input->post('content');
        $ret = $this->Admin->addPost($data);
        echo $ret;
    }

    // 修改文章
    public function edit_post($post_id){
        // 分类数据
        $data['cats'] = $this->db->get_where('ci_cat_config', array('cat_status' => 1))->result_array();
        // 标签数据
        $data['tags'] = $this->db->get_where('ci_tags_config', array('tag_status' => 1))->result_array();
        // 博文数据
        $data['post'] = $this->db->get_where('ci_post', array('post_id' => $post_id))->row_array();
        $this->load->view('/admin/edit_post',$data);
    }

	public function update_post(){
		$data['post_id'] = $this->input->post('id');
	    $data['post_title'] = $this->input->post('title');
        $data['cat_id'] = $this->input->post('cat');
        $tags = $this->input->post('tag');
        $tags == TRUE ? $data['tag_id'] = implode(',', $tags) : $data['tag_id'] = '';
        $data['post_content'] = $this->input->post('content');
        $ret = $this->Admin->updatePost($data);
        echo $ret;
	}

    // 删除文章
    public function ajax_del_post() {
        $post_id = $this->input->post('post_id');
        $this->db->where('post_id', $post_id);
        $ret = $this->db->update('ci_post', array('post_status' => 0));
        echo $ret;
    }

    // 博文内容图片上传处理
    public function post_upload() {
        if ($_FILES['file']['name']) {
            if (!$_FILES['file']['error']) {
                $name = md5(rand(100, 200));
                $ext = explode('.', $_FILES['file']['name']);
                $filename = $name . '.' . $ext[1];
                $destination = '/uploads/post/' . $filename;
                $location = $_FILES["file"]["tmp_name"];
                move_uploaded_file($location, $destination);
                echo $filename;
            } else {
                echo $message = 'Ooops!  Your upload triggered the following error:  ' . $_FILES['file']['error'];
            }
        }
    }

    // 图片管理
    public function gallery_config() {
        $gallery_data['gallery'] = $this->Admin->getGalleryConfig();
        $this->load->view('/admin/gallery_config', $gallery_data);
    }

    public function ajax_del_gallery() {
        $gallery_id = $this->input->post('gallery_id');
        $this->db->where('gallery_id', $gallery_id);
        $ret = $this->db->update('ci_gallery', array('photo_status' => 0));
        echo $ret;
    }

    public function ajax_open_gallery() {
        $gallery_id = $this->input->post('gallery_id');
        $this->db->where('gallery_id', $gallery_id);
        $ret = $this->db->update('ci_gallery', array('photo_status' => 1));
        echo $ret;
    }

    public function ajax_close_gallery() {
        $gallery_id = $this->input->post('gallery_id');
        $this->db->where('gallery_id', $gallery_id);
        $ret = $this->db->update('ci_gallery', array('photo_status' => 2));
        echo $ret;
    }

}
